// pages/goods_detail/index.js
import { request } from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productListData: {},
    isCollection: false
  },
  goods_info: {
    goods_id: ''
  },

  //保存数据的公共对象
  ListData: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

  },

  //获取商品列表的数据
  getProductListData() {
    request({
      url: '/goods/detail',
      data: this.goods_info
    }).then(res => {
      this.ListData = res.data.message
      this.setData({
        productListData: res.data.message
      })
      //获取收藏的缓存数组
      let collection = wx.getStorageSync('collection') || []
      //判断
      let isCollection = collection.some(v => v.goods_id === this.ListData.goods_id)
      this.setData({
        isCollection
      })
    }).catch(err => {
      console.log(err + "sss")
    })
  },

  //点击图片放大效果
  handleTap(e) {
    //获取当前显示图片的链接
    let current = e.currentTarget.dataset.url
    //获取需要预览的图片列表
    let urls = this.ListData.pics.map(v => v.pics_mid)
    wx.previewImage({
      current: current,// 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },

  //购物车添加方法
  cartAdd() {
    //第一步获取缓存中的购物车数据
    let cart = wx.getStorageSync('cart') || [];
    //第二步判断
    let index = cart.findIndex(v => v.goods_id === this.ListData.goods_id)
    if (index === -1) {
      //没有数据
      this.ListData.num = 1;
      this.ListData.isCheck = false;
      this.ListData.time = Date.now()
      cart.push(this.ListData)
    } else {
      cart[index].num++
    }
    wx.setStorageSync("cart", cart)
    //样式
    wx.showToast({
      title: '加入成功',
      mask: true,
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPages = pages[pages.length - 1]
    let goods_id = currentPages.options.goods_id
    console.log(goods_id)

    this.goods_info.goods_id = goods_id
    this.getProductListData()
  },

  //设置点击收藏商品并添加到缓存中
  handleIsCollection() {
    let isCollection = false
    //提取出缓存中的数组
    let collection = wx.getStorageSync('collection') || []

    //判断
    let index = collection.findIndex(v => v.goods_id === this.ListData.goods_id)
    if (index === -1) {
      //没有则是添加
      collection.push(this.ListData)
      isCollection = true
      wx.showToast({
        title: '收藏成功',
        icon: "success",
        mask: true
      })
    } else {
      //有则删除
      collection.splice(index, 1)
      isCollection = false
      wx.showToast({
        title: '取消成功',
        icon: "success",
        mask: true
      })
    }

    wx.setStorageSync('collection', collection)

    //修改isCollection的值
    this.setData({
      isCollection
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})